﻿using System;
using System.Data;
using System.Windows.Forms;

namespace HSIA
{
    public partial class FrmUserCheck : Form
    {
        DatabaseObject dbo;
        //DataTable dtUsers; 
        string strConn;

        public FrmUserCheck(DatabaseObject vdbo, string strConn)
        {
            this.dbo = vdbo;
            this.strConn = strConn;
            InitializeComponent();
            //tnName.Text = dbo.drLoginUser["UserID"].ToString();
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            if (CheckPassword()) this.DialogResult = DialogResult.OK;
        }

        private bool CheckPassword()
        {
            if (tnName.Text.Trim() == "")
            {
                MessageBox.Show(dbo.GetLanguage(333), dbo.GetLanguage(141), MessageBoxButtons.OK, MessageBoxIcon.Information);
                tnName.Focus();//"用户名不能为空!", "提示", 
                return false;
            }
            else
            {
                if (tnPassword.Text.Trim() == "")
                {
                    MessageBox.Show(dbo.GetLanguage(202), dbo.GetLanguage(141), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    tnPassword.Focus();//"密码不能为空!", "提示", 
                    return false;
                }
                else
                {
                    DataTable dtUsers = dbo.GetDataTableBySql("Select * From Sys_Users Where UserName='" + tnName.Text.Trim() + "'", "Users", strConn);
                    if (dtUsers.Rows.Count != 0)
                    {
                        string md5pass = MD5string(tnPassword.Text.Trim() + dtUsers.Rows[0]["PassSalt"]);
                        if (dtUsers.Rows[0]["PassWord"].ToString().Equals(md5pass, StringComparison.OrdinalIgnoreCase) &&
                            Convert.ToBoolean(dtUsers.Rows[0]["IsAdmin"]))
                        {
                            return true;
                        }
                        else
                        {
                            MessageBox.Show(dbo.GetLanguage(332), dbo.GetLanguage(141), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            tnName.Clear();//"用户名和密码错误,请重新输入!", "提示",
                            tnPassword.Clear();
                            tnName.Focus();
                        }
                    }
                    else
                    {
                        MessageBox.Show(dbo.GetLanguage(332), dbo.GetLanguage(141), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        tnName.Clear();//"用户名和密码错误,请重新输入!", "提示",
                        tnPassword.Clear();
                        tnName.Focus();
                    }
                }
            }
            return false;
        }

        private void FrmPlaceEdit_KeyDown(object sender, KeyEventArgs e)
        {
            //if((e.Control)&&(e.KeyCode==Keys.Enter)) button1_Click(null,null);
        }

        private void FrmPasswordEdit_Load(object sender, EventArgs e)
        {
            tnName.Focus();
            this.Text = dbo.GetLanguage(38);
            button1.Text = dbo.GetLanguage(193); //193确定
            button2.Text = dbo.GetLanguage(51);//51取消
            label1.Text = dbo.GetLanguage(180);
            label2.Text = dbo.GetLanguage(204);
        }

        private void tnold_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) tnPassword.Focus();
        }

        private void tnnew_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) btOK_Click(null, null);
        }

        public static string MD5string(string str)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        }

    }
}
